package com.my.attendance.controller;

import com.my.attendance.entity.Employee;
import com.my.attendance.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/check")
public class CheckLoginServlet extends HttpServlet {
    private UserService userService=new UserService();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            req.setCharacterEncoding("utf-8");
            resp.setContentType("text/html;charset=utf-8");
            String employee_number = req.getParameter("employee_number");
            String password = req.getParameter("password");
            Employee employee = userService.checkLogin(employee_number, password);
            if (employee.getId()!=null){
                req.getSession().setAttribute("userinfo",employee);
                req.getRequestDispatcher("go").forward(req,resp);
            }else {
                req.setAttribute("msg","账号未注册，无法登录");
                req.getRequestDispatcher("/jsp/login.jsp").forward(req,resp);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
